package com.zs.grain.ui

import android.app.ProgressDialog
import android.content.ComponentName
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.zs.grain.*
import com.zs.grain.databinding.FragmentDailyBinding
import com.xnkj1688.semplibrary.mvvm.*
import com.xnkj1688.semplibrary.recyclerview.ItemView

@Route(path = ARouterPath.DailyFragment)
class DailyFragment : BaseMvvmFragment<FragmentDailyBinding, DailyViewModel>() {
    override val layoutId = R.layout.fragment_daily

    override fun initViewModel(binding: FragmentDailyBinding) {
        binding.vm = this.mViewModel
        binding.view = this

    }

    val logItemView = ItemView<String>(BR.vm, R.layout.item_list4)

    override fun onUiNotify(name: String, bundle: Bundle) {
        super.onUiNotify(name, bundle)
        when (name) {
            "ShowProgress" -> {
                showProgressDialog("产品测试", "测试中......")
            }
            "HideProgress" -> {
                hideProgressDialog()
            }
            "ShowToast" -> {
                var text = bundle.getString("text")
                var showTime = bundle.getInt("howLong")//Toast.LENGTH_LONG
                Toast.makeText(context,text,showTime).show()
            }
        }
    }

    internal var progressingView: ProgressDialog? = null
    // 显示进度
    fun showProgressDialog(title: String, msg: String) {
        if (TextUtils.isEmpty(msg) || TextUtils.isEmpty(title)) {
            return
        }
        if (progressingView == null) {
            progressingView = ProgressDialog(this.activity)//1.创建一个ProgressDialog的实例
        }
        progressingView!!.setTitle(title)//2.设置标题
        progressingView!!.setMessage(msg)
//        progressingView!!.setCancelable(false)//4.设置可否用back键关闭对话框
        progressingView!!.setCanceledOnTouchOutside(false)
        progressingView!!.show()
    }

    fun hideProgressDialog() {
        if (progressingView == null) {
            return
        }
        progressingView!!.hide()
    }

    override fun onResume() {
        super.onResume()
        this.mViewModel.startStateTimer()
    }

    override fun onPause() {
        super.onPause()
        this.mViewModel.stopStateTimer()
    }

    fun onOpenBigDoor() {
        CommonDialog.Builder(this.context!!)
                .setPositiveButton("是", DialogInterface.OnClickListener { p0, p1 ->
                    p0?.dismiss()
                    this.mViewModel.openBigDoor()
                })
                .setNegativeButton("否", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() })
                .setWith(0.77f)
                .create(true,R.layout.dialog_yes_no,R.id.yesButton,R.id.noButton)
                .show()
    }

    fun onOpenSmallDoor() {
        CommonDialog.Builder(this.context!!)
                //.setImageHeader(R.mipmap.logo)
                //.setTitle("你是否要打开小门")
                //.setMessage("注销后需重新注册才能使用牛返返优惠")
                .setPositiveButton("是", DialogInterface.OnClickListener { p0, p1 ->
                    p0?.dismiss()
                    this.mViewModel.openSmallDoor()
                })
                .setNegativeButton("否", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() })
                .setWith(0.77f)
                .create(true,R.layout.dialog_yes_no,R.id.yesButton,R.id.noButton)
                .show()
    }

    fun onDynTest(){
        CommonDialog.Builder(this.context!!)
                .setPositiveButton("是", DialogInterface.OnClickListener { p0, p1 ->
                    p0?.dismiss()
                    this.mViewModel.dynTest()
                })
                .setNegativeButton("否", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() })
                .setWith(0.77f)
                .create(true,R.layout.dialog_yes_no,R.id.yesButton,R.id.noButton)
                .show()
    }

    fun onRushTest(){
        CommonDialog.Builder(this.context!!)
            .setPositiveButton("是", DialogInterface.OnClickListener { p0, p1 ->
                p0?.dismiss()
                this.mViewModel.rushTest()
            })
            .setNegativeButton("否", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() })
            .setWith(0.77f)
            .create(true,R.layout.dialog_yes_no,R.id.yesButton,R.id.noButton)
            .show()
    }

    fun onFillWater() {
        CommonDialog.Builder(this.context!!)
                .setPositiveButton("是", DialogInterface.OnClickListener { p0, p1 ->
                    p0?.dismiss()
                    this.mViewModel.fillWater()
                })
                .setNegativeButton("否", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() })
                .setWith(0.77f)
                .create(true,R.layout.dialog_yes_no,R.id.yesButton,R.id.noButton)
                .show()
    }

    fun onFillCup() {
        CommonDialog.Builder(this.context!!)
                .setPositiveButton("是", DialogInterface.OnClickListener { p0, p1 ->
                    p0?.dismiss()
                    this.mViewModel.fillCup()
                })
                .setNegativeButton("否", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() })
                .setWith(0.77f)
                .create(true,R.layout.dialog_yes_no,R.id.yesButton,R.id.noButton)
                .show()
    }

    fun onDistinct(){
        CommonDialog.Builder(this.context!!)
            .setPositiveButton("是", DialogInterface.OnClickListener { p0, p1 ->
                p0?.dismiss()
                this.mViewModel.distinct()
            })
            .setNegativeButton("否", DialogInterface.OnClickListener { p0, p1 -> p0?.dismiss() })
            .setWith(0.77f)
            .create(true,R.layout.dialog_yes_no,R.id.yesButton,R.id.noButton)
            .show()
    }

    fun onQuit() {
        var homeIntent = Intent(Intent.ACTION_MAIN, null)
        homeIntent.addCategory(Intent.CATEGORY_HOME)
        val list = this.activity!!.packageManager.queryIntentActivities(homeIntent, 0)
        val info = list[0]
        val pkg = info.activityInfo.packageName
        val cls = info.activityInfo.name
        val componet = ComponentName(pkg, cls)
        homeIntent = Intent()
        homeIntent.component = componet
        startActivity(homeIntent)
        ViewManager.instance.exitApp(this.activity!!)
        System.exit(0);
    }

    fun onExit() {
        ViewManager.instance.finishActivity()
    }

    companion object {

        val TAG = "DailyFragment"

    }
}
// Required empty public constructor
